// pages/dt/dt.js

import {getQuestion,addScore,getScore} from "../../api/api"
const VALUES = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N"];
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    questions:[],
    question:{},
    num:0,
    score:0,
    id:0,
    disable:false,
    phb:[],
    phbShow:false,
    complateStatus:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    let index = Number(options.index) || 0;
    let id = Number(options.id);
    this.setData({id});
    this.loadData(id,index);
    this.getScore(id);
  },
  loadData(id,index = 0){
    getQuestion({id:id}).then(res=>{
      let list = res.data;
      list = list.map(el=>{
        if(el.type === 0){
          el.checks = el.value.split("#").map(ev=>{
            return {
              value:ev,
              status:0
            }
          });
          el.userData = "";
        }else if(el.type === 1){
          el._title = el.title.split("#").map(ev=>{
            return {
              value:ev,
              status:0
            }
          })
        }
        return el;
      });
      console.log(list[index])
      this.setData({
        list,
        question:list[index],
        num:index
      });
    })
  },
  handleSelect(event){
    if(this.data.disable){
      return
    }
    let index = event.currentTarget.dataset.index;
    let userAnswer = VALUES[index];
    let {score,questions,list,question,id} = this.data;
    question.userData = userAnswer;
    if(userAnswer === question.answer){
      question.checks[index].status = 1;
    }else{
      question.checks[index].status = 2;
    }
    this.setData({
      userAnswer,
      question,
      disable:true
    })
    // app.globalData.questions.push(question);
    let num = Number(this.data.num)+1;
    questions.push(question);
    if(num<=this.data.list.length-1){
      setTimeout(() => {
        this.setData({
          disable:false,
          num:num,
          question:list[num],
          questions,
          score:this.getSorce(questions)
        });
        wx.pageScrollTo({
          duration: 100,
          scrollTop:0
        })
      }, 300);
    }else{
      console.log("complate");
      let _score = this.getSorce();
      let {openid,userinfo} = app.globalData;
      // wx.showToast({
      //   title: '闯关分数：'+_score,
      //   icon:"none"
      // });
      this.setData({
        score:_score
      })
      addScore({
        ...userinfo,
        openid,
        score:_score,
        moduleId:id
      }).then(res=>{
        this.setData({
          complateStatus:true
        })
      })
    }

   
  },
  handleInput(event){
    console.log(event)
  },
  getSorce(questions = this.data.questions){
    let totalScore = 0;
    questions.forEach(el=>{
      if(el.userData === el.answer){
        totalScore+=el.score;
      }
    });
    return totalScore;
  },
  getScore(moduleId=this.data.id){
    getScore({moduleId}).then(res=>{
      console.log(res,"res");
      let list = res.data.splice(0,10);
      this.setData({
        phb:list
      })
    })
  },
  showPhb(){
    this.setData({
      phbShow:true
    })
  },
  showDfl(){
    this.setData({
      dflShow:true
    })
  },
  closePhb(){
    this.setData({
      phbShow:false
    })
  },
  closeDfl(){
    this.setData({
      dflShow:false
    })
  },
  back(event){
    console.log("back");
    wx.navigateBack({
      delta: 0,
    })
  }
})